from framwork.common.tools  import open_browsers
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

"""逆向：邮件发送"""
#数据
url=r"http://192.168.1.23/crm/index.php?m=user&a=login"
user_name="admin"
passwd="123456"
email="sun03325@163.com"
content="今天是8月24号。"
#定位器
username_locator=(By.NAME,"name")
password_locator=(By.NAME,"password")
login_btn_locator=(By.NAME,"submit")
gd_locator=(By.LINK_TEXT,"更多")
yx_locator=(By.LINK_TEXT,"营销")
sendemail_locator=(By.LINK_TEXT,"发送邮件")
emails_locator=(By.NAME,"emails")
selectmail_locator=(By.NAME,"body > div.container > div.row > div:nth-child(2) > form > div > div:nth-child(2) > p > a:nth-child(2)")
mailmodel_locator=(By.NAME,"template")
mailtitle_locator=(By.NAME,"title")
mailcontent_locator=(By.CSS_SELECTOR,"body")
btn_locator=(By.CSS_SELECTOR,"body > div.container > div.row > div:nth-child(2) > form > div > div:nth-child(2) > div:nth-child(3) > input")
#操作
driver=open_browsers()
driver.get(url)
sleep(3)
driver.find_element(*username_locator).send_keys(user_name)
driver.find_element(*password_locator).send_keys(passwd)
driver.find_element(*login_btn_locator).click()
sleep(1)
driver.find_element(*gd_locator).click()
driver.find_element(*yx_locator).click()
driver.find_element(*sendemail_locator).click()
driver.find_element(*emails_locator).send_keys(email)
mailmodel=driver.find_element(*mailmodel_locator)
mailmodel_obj=Select(mailmodel)
mailmodel_obj.select_by_visible_text("国庆快乐")
WebDriverWait(driver, 10).until(
    EC.frame_to_be_available_and_switch_to_it(
        (By.CSS_SELECTOR, "iframe.ke-edit-iframe")
    )
)
driver.switch_to.default_content()
driver.find_element(*btn_locator).click()
sleep(1)
#退出浏览器
sleep(3)
driver.close()